%% split data from G-econ into countries and into connected countries in Europe
% identify each cell in Gecon with a country in our data

clear
close all
clc

set(groot,'defaultFigurePaperPositionMode','manual')
set(0,'DefaultFigureWindowStyle','docked','DefaultFigureVisible','on')

% -----------------------
% DEFINE FOLDER LOCATIONS
folders;

%% transform G-Econ dataset into map structure
gecon_data = csvread([datafolder_gecon,'gecon_data.csv']);

lat = gecon_data(:,1);
lon = gecon_data(:,2);
pop = gecon_data(:,3);
Y = gecon_data(:,4);

N = length(Y);

countries = importdata([datafolder_gecon,'gecon_countries.csv']);

for j=1:N
    
    places_gecon(j).Geometry = 'Polygon';
   
    places_gecon(j).X = [ lon(j) lon(j)   lon(j)+1 lon(j)+1  NaN];   % lon(j), lat(j) is the SW corner
    places_gecon(j).Y = [ lat(j) lat(j)+1 lat(j)+1 lat(j)    NaN];

    places_gecon(j).Xcorner = lon(j); % lon(j), lat(j) is the SW corner
    places_gecon(j).Ycorner = lat(j);
    
    places_gecon(j).population = pop(j);
    places_gecon(j).gdp = Y(j);
    places_gecon(j).country = countries(j);
    
end

save([datafolder_gecon,'places_gecon'])

%% load country list

country_list_short;

%% loop through all countries

for country_n=1:Ncountries

    %%
    country_icc = char( countries(country_n) );  % country ICC code
    country = icc2name( country_icc );           % country name

    if ~ispc()   %% we are in a mac

        % folder to save the outcome
        datafolder_LAC_country =  [ datafolder_LAC,country_icc,'/'];

    else   %% we are in a pc   

        % folder to save the outcome
        datafolder_LAC_country =  [ datafolder_LAC,country_icc,'\' ];

    end
    
    clc
    disp( ['Country: ',country] )   
    
 %% %%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%% bring income data %%% 
    %%%%%%%%%%%%%%%%%%%%%%%%%%
    
    % bring all G-Econ
    file  = [datafolder_gecon,'places_gecon.mat'];
    temp = load(file);
    places_gecon = temp.places_gecon;
      
    % keep a cell in G-Econ if:
    % it has the same country name as the chosen country
    % in this case, always the USA
    keep = zeros( length(places_gecon),1 );
    for j=1:length(places_gecon) % United States in this case
            keep(j) = strcmp( places_gecon(j).country,country );
    end
    places_gecon = places_gecon( logical(keep) );
  
    
    % further restrict to cells that overlap with country boundaires
    file  = [ datafolder_LAC_country,'country_bounds.mat'];
    load(file);
    keep = zeros( length(places_gecon),1 );
    for j=1:length(places_gecon)
          keep(j) = ~isempty( polybool( 'intersection',...
                              cell2mat({places_gecon(j).X}), cell2mat({places_gecon(j).Y}),...
                              country_bounds.X, country_bounds.Y ) );

     end
     places_gecon = places_gecon( logical(keep) );

    % save
    file  = [ datafolder_LAC_country,'places_gecon.mat'];
    save(file,'places_gecon'); 
    
    %% plot
   
    h=figure;
    
    mapshow(places_gecon,'FaceColor','white')
    mapshow(country_bounds,'FaceColor','none')
    for i=1:length(places_gecon)
        text( places_gecon(i).Xcorner+1/2,places_gecon(i).Ycorner+1/2,...
            num2str( round(places_gecon(i).gdp*10)/10 ),...
            'color','red')
    end
    
    title(country) 
    if ismac
        saveas(h, [datafolder_gecon,'maps/',country,'_map_gecon.pdf']) 
    else
        saveas(h, [datafolder_gecon,'maps\',country,'_map_gecon.pdf']) 
    end
end



